const router = require('express').Router()
const { getAllServices, getServiceWithCondition, delServiceWithCondition } = require('../cse/microservice')
const { getInstanceWithCondition } = require('../cse/instance')

router.get('/microservice/all', (req, res) => {
  const { ip } = req.query
  getAllServices(ip).then(data => {
    res.send(data)
  })
})

router.get('/microservice/condition', (req, res) => {
  const { ip, appId: cAppId, serviceName: cServiceName, version: cVersion, env: cEnv, schema: cSchema } = req.query
  getServiceWithCondition(ip, { cAppId, cServiceName, cVersion, cEnv, cSchema }).then(data => {
    res.send(data)
  })
})

router.delete('/microservice/condition', (req, res) => {
  const { ip, appId: cAppId, serviceName: cServiceName, version: cVersion, env: cEnv, schema: cSchema, force } = req.query
  delServiceWithCondition(ip, { cAppId, cServiceName, cVersion, cEnv, cSchema }, force).then(data => {
    res.send(data)
  })
})

router.get('/instance/all', (req, res) => {
  const { ip, appId, serviceName, version } = req.query
  getInstanceWithCondition(ip, appId, serviceName, version).then(data => {
    res.send(data)
  })
})

module.exports = router
